﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Client.Configuration;
using ZhimeiSoft.Framework.Client.Base;

namespace ZhimeiSoft.CharmManager.ClientForms.Control.Scheduling
{
    public partial class frmSetComPort : FormBase
    {
        private delegate void MyDelegate(string comPortStr);

        private ClientConfiguration clientConfig = new ClientConfiguration();
        private string m_showPhoneCmd = "AT+VCID=1" + "\r" + "at#cid=1" + "\r";

        public frmSetComPort()
        {
            InitializeComponent();
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            comboBoxCom.Items.Add("未选");
            foreach (string aPort in ports)
            {
                int idx = comboBoxCom.Items.IndexOf(aPort);
                if (idx == -1)
                {
                    comboBoxCom.Items.Add(aPort);
                }
            }
        }

        private void InitModem()
        {
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();

            foreach (string str in ports)
            {
                SerialPort sp = new SerialPort(str, 9600, Parity.None, 8, StopBits.One);
                sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                try
                {
                    sp.Open();
                    sp.Write(m_showPhoneCmd);// ("AT+VCID=1" + "\r");
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sp.Close();

                }
                
            }
        }

        private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort tmpSp = sender as SerialPort;
            byte[] bt = new byte[256];
            string rd = string.Empty;

            tmpSp.Read(bt, 0, bt.Length);
            rd = UnicodeEncoding.ASCII.GetString(bt);
            if (rd.Contains("OK"))
            {
                try
                {
                    comboBoxCom.BeginInvoke(new MyDelegate(updateComBoxComPort), new object[] { tmpSp.PortName });

                }
                catch (Exception ex)
                {
                    MsgBox.MsgInfo(ex.Message);
                }
                finally
                {
                    tmpSp.Close();
                }
            }
        }

        private void updateComBoxComPort(string comPort)
        {
            //string aPortStr = comPort.ToUpper().Replace("COM", "").Trim();
            //Int32 idx;
            
           // if (!Int32.TryParse(aPortStr, out idx))
                //idx = 0;
            Int32 idx = comboBoxCom.Items.IndexOf(comPort.ToUpper().Trim());
            if (idx == -1)
            {
                idx = 0;
            }
            comboBoxCom.SelectedIndex = idx;


           // comboBoxCom.SelectedIndex = idx;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {

            if (comboBoxCom.SelectedIndex == -1 || comboBoxCom.SelectedItem.Equals("未选"))
            {
                MsgBox.MsgInfo("请选择正确的端口。");
                return;
            }
            clientConfig.SetAppSettings("ComPort", comboBoxCom.SelectedItem.ToString());
            this.DialogResult = DialogResult.OK;
            //this.Close();
        }

        private void buttonAutoQuery_Click(object sender, EventArgs e)
        {
            InitModem();
            MsgBox.MsgInfo("搜索完毕。");
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmSetComPort_Shown(object sender, EventArgs e)
        {
            updateComBoxComPort(clientConfig.ComPort);
        }

    }
}
